﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtyManagementSystem.BLL;
using RealtyManagementSystem.DAL;

namespace RealtyManagementSystem.Web.RealtyManagers
{
    public partial class AddHouseholder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
            }
        }

        private void BindControls()
        {
            var buildings = new BuildingsProvider();
            DropDownList ddlBuildings
                = (DropDownList)templateWizardStepHouseInfo.ContentTemplateContainer.FindControl("ddlBuildingNumber");
            ddlBuildings.DataSource = buildings.GetBuildings();
            ddlBuildings.DataValueField = "BuildingId";
            ddlBuildings.DataTextField = "BuildingName";
            ddlBuildings.DataBind();

            var ddlUnit = (DropDownList)templateWizardStepHouseInfo.ContentTemplateContainer.FindControl("ddlUnitNumber");
            var ddlNumber = (DropDownList)templateWizardStepHouseInfo.ContentTemplateContainer.FindControl("ddlNumber");
            for (int i = 1; i <= 5; i++)
            {
                var li = new ListItem(i.ToString(), i.ToString());
                ddlUnit.Items.Add(li);

                var itemHouse01 = new ListItem(i + "01", i + "01");
                var itemHouse02 = new ListItem(i + "02", i + "02");
                ddlNumber.Items.Add(itemHouse01);
                ddlNumber.Items.Add(itemHouse02);
            }
            ddlNumber.DataBind();
            ddlUnit.DataBind();
        }

        protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            Roles.AddUserToRoles(CreateUserWizard1.UserName, new string[] { "Householders" });
        }

        protected void CreateUserWizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
        }

        protected void CreateUserWizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            var txtUsrName = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName");
            MembershipUser user = Membership.GetUser(txtUsrName.Text);

            var ddlBuildings
                = (DropDownList)templateWizardStepHouseInfo.ContentTemplateContainer.FindControl("ddlBuildingNumber");
            var ddlUnit = (DropDownList)templateWizardStepHouseInfo.ContentTemplateContainer.FindControl("ddlUnitNumber");
            var ddlNumber = (DropDownList)templateWizardStepHouseInfo.ContentTemplateContainer.FindControl("ddlNumber");
            var txtName = (TextBox)templateWizardStepHouseInfo.ContentTemplateContainer.FindControl("txtName");
            var txtPhone = (TextBox)templateWizardStepHouseInfo.ContentTemplateContainer.FindControl("txtPhone");
            var txtId = (TextBox)templateWizardStepHouseInfo
                .ContentTemplateContainer.FindControl("txtId");
            var txtOtherMembers = (TextBox)templateWizardStepHouseInfo
                .ContentTemplateContainer.FindControl("txtOtherMembers");
            var txtCorporation = (TextBox)templateWizardStepHouseInfo
                .ContentTemplateContainer.FindControl("txtCorporation");

            var hd = new Household()
            {
                HouseholdID = (Guid)user.ProviderUserKey,
                BuildingId = Convert.ToInt16(ddlBuildings.SelectedValue),
                UnitNo = Convert.ToByte(ddlUnit.SelectedValue),
                DepartmentNo = Convert.ToInt16(ddlNumber.SelectedValue),
                HouseHolder = txtName.Text,
                IdNumber = txtId.Text,
                Telephone = txtPhone.Text,
                Corporation = txtCorporation.Text,
                OtherHouseMembers = txtOtherMembers.Text
            };

            using (HouseholdProvider hdprovider = new HouseholdProvider())
            {
                hdprovider.Add(hd);
            }
        }
    }
}